package com.omt.tarjimdinek.domain.member.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import com.google.common.base.Objects;
import com.omt.tarjimdinek.domain.islamicLibrary.entity.Sahabi;
import com.omt.tarjimdinek.domain.message.entity.DiscussionPrive;
import com.omt.tarjimdinek.domain.translate.entity.DemandeTraduction;
import com.omt.tarjimdinek.support.Identifiable;

/**
 * member of social network
 * @author chabane
 *
 */
@Entity
@Table(name="a01_mbr")
public class Membre implements Identifiable<Long>, Serializable{

    private static final long serialVersionUID = 1L;
    private static final Logger log = Logger.getLogger(Membre.class);
    
	/** ID SGBD */
	@Column(name="CNMBR", length=11)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(name = "VERSION", precision = 10)
    @Version
    private Integer version;
	
	/** civility */
	@Size(max = 10)
	@Column(name="LICIV", length=10)
	private String civilite;
	
	/** first name */
	@Size(max = 45)
	@Column(name="LIMBR1", length=45)
	private String nom;
	
	/** last name */
	@Size(max = 45)
	@NotEmpty
	@Column(name="LIMBR2", length=45, nullable=false)
	private String prenom;
	
	/** birthday */
	@Temporal(TemporalType.DATE)
	@NotNull
	@Column(name="LIDN", length=45, nullable=false)
	private Date dateNaissance;
	
	/** email */
	@Size(max = 45)
	@NotEmpty
	@Column(name="LIMAIL", length=45, nullable=false)
	@Email
	private String email;
	
	/** address */
	@NotNull
	@JoinColumn(name="CNADR", nullable=false)
    @ManyToOne
	private Adresse adresse;
	
	/** function */
	@JoinColumn(name="CNFCT")
	@ManyToOne
	private Fonction fonction;
	
	/** languages speaking */
	@JoinTable(name = "a01_lng_mbr", 
			joinColumns = @JoinColumn(name = "CNMBR"), 
			inverseJoinColumns = @JoinColumn(name = "CNLNG"))
	@ManyToMany(fetch=FetchType.LAZY)
	private List<Langue> listeLangues;
	
    @OneToMany(mappedBy="membre", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
	private List<Notification> listeNotifications;
	
    @OneToMany(mappedBy="demandeur", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
	private List<DemandeTraduction> listeDemandesTraduction;
    
    @OneToMany(mappedBy="initiateur", fetch=FetchType.LAZY, cascade = CascadeType.ALL)
	private List<DiscussionPrive> listeDiscussionsPrivees;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getCivilite() {
		return civilite;
	}

	public void setCivilite(String civilite) {
		this.civilite = civilite;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public Date getDateNaissance() {
		return dateNaissance;
	}

	public void setDateNaissance(Date dateNaissance) {
		this.dateNaissance = dateNaissance;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Adresse getAdresse() {
		return adresse;
	}

	public void setAdresse(Adresse adresse) {
		this.adresse = adresse;
	}

	public Fonction getFonction() {
		return fonction;
	}

	public void setFonction(Fonction fonction) {
		this.fonction = fonction;
	}

	public List<Langue> getListeLangues() {
		return listeLangues;
	}

	public void setListeLangues(List<Langue> listeLangues) {
		this.listeLangues = listeLangues;
	}

	public List<Notification> getListeNotifications() {
		return listeNotifications;
	}

	public void setListeNotifications(List<Notification> listeNotifications) {
		this.listeNotifications = listeNotifications;
	}

	public List<DemandeTraduction> getListeDemandesTraduction() {
		return listeDemandesTraduction;
	}

	public void setListeDemandesTraduction(
			List<DemandeTraduction> listeDemandesTraduction) {
		this.listeDemandesTraduction = listeDemandesTraduction;
	}
	
	public List<DiscussionPrive> getListeDiscussionsPrivees() {
		return listeDiscussionsPrivees;
	}

	public void setListeDiscussionsPrivees(
			List<DiscussionPrive> listeDiscussionsPrivees) {
		this.listeDiscussionsPrivees = listeDiscussionsPrivees;
	}

	@Transient
	public boolean isIdSet() {
		return id != null && id.compareTo(Long.valueOf(0)) != 0;
	}
	
    /**
     * Set the default values.
     */
    public void initDefaultValues() {
    }
    
	/**
     * equals implementation using a business key.
     */
    public boolean equals(Object other) {
        return this == other || (other instanceof Sahabi && hashCode() == other.hashCode());
    }

    public int hashCode() {
        int hashCode = Objects.hashCode(getEmail());
        return hashCode;
    }
    
    /**
     * Construct a readable string representation for this instance.
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return Objects.toStringHelper(this) //
                .add("id", getId()) //
                .add("email", getEmail()) //
                .add("nom", getNom()) //
                .add("prenom", getPrenom()) //
                .add("civilite", getCivilite()) //
                .add("dateNaissance", getDateNaissance()) //
                .add("adresse", getAdresse()) //
                .add("fonction", getFonction()) //
                .add("version", getVersion()) //
                .toString();
    }
}
